package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @DeleteMapping("/{id}")
    public Result CourseDelete(@PathVariable Integer id){
        courseService.CourseDelete(id);
        return Result.success();
    }


    @GetMapping
    public Result CoursePageList(Integer subject, String name, Integer target,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Course> coursepageResult = courseService.getPageList(subject,name,target,page,pageSize);
        return Result.success(coursepageResult);
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result findCourseById(@PathVariable Integer id){
        Course course = courseService.findCourseById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result courseList(Course course){
        List<Course> courseList = courseService.getCourseList(course);
        return Result.success(courseList);
    }

    @GetMapping("/subject/{subject}")
    public Result getCourseBySubject(@PathVariable Integer subject){
        List<Course> subjectCourse = courseService.getCourseBySubject(subject);
        return Result.success(subjectCourse);

    }

}
